#!/bin/bash
echo "#! /usr/bin/tclsh8.5"

echo "read_verilog $HSTA_BENCHMARK_DIR/$HSTA_BENCHMARK.v"
echo "current_design $HSTA_BENCHMARK"
echo "link_design $HSTA_BENCHMARK"
echo

HSTA_PG_OUT_FILE=$HSTA_BENCHMARK_DIR/$HSTA_BENCHMARK".out"
awk '{if(/create_power_group/) { \
print $0;
printf("create_power_domain PD_%s -element [get_power_group_objects %s]\n", $3, $3);
match($3,"region"); temp=substr($3,RSTART+6);
printf("create_supply_net VN%s -domain PD_%s\n", temp, $3);
printf("create_supply_net GN%s -domain PD_%s\n", temp, $3);
printf("set_domain_supply_net PD_%s -primary_power_net VN%s -primary_ground_net GN%s\n\n", $3, temp, temp)
}}' $HSTA_TCL_FILE

array=($(awk '/create_power_group/ { \
match($3,"region"); temp=substr($3,RSTART+6); print temp}' $HSTA_TCL_FILE))

#echo "region count (for verification): "${#array[@]}

for var in "${array[@]}"
do 
    regName="region"$var
    awk -v dim=$HSTA_REG_CNT -v region_id=$var 'BEGIN{node_per_layer=dim+region_id} {if(NR==region_id) printf("set_voltage %s -object_list GN%s\n", $1, region_id)
    if(NR==node_per_layer) printf("set_voltage %s -object_list VN%s\n", $1, region_id)}' $HSTA_PG_OUT_FILE
    ## Temp should actually be 300
    ## if temp is considered
    #awk -v region_name=$regName '$0 ~ region_name {derate_factor=($2/320)^1.5; printf("set_timing_derate -late -cell_delay %s [get_power_group_objects %s]\n", derate_factor, region_name); exit}' $HSTA_STEADY_FILE
done
echo

# echo "create_power_domain PD_region1 -element [get_power_group_objects region1]"
# echo "create_supply_net VN1 -domain PD_region1"
# echo "create_supply_net GN1 -domain PD_region1"
# echo "set_domain_supply_net PD_region1 -primary_power_net VN1 -primary_ground_net GN1"
# echo "set_voltage 0.79 -object_list VN1"
# echo "set_voltage 0.11 -object_list GN1"
# echo "set_temperature -object_list [get_power_group_objects region1] 15"
# echo "set_temperature -object_list [get_power_group_objects region3] 115"

echo "read_sdc $HSTA_BENCHMARK_DIR/$HSTA_BENCHMARK.sdc"
#echo "read_parasitics -format SPEF $HSTA_BENCHMARK_DIR/$HSTA_BENCHMARK.spef"
echo
echo "source pt_scripts.tcl"
echo "exit"